package com.shk.controller;

        import com.alibaba.dubbo.config.annotation.Reference;
        import com.shk.annotation.Log4jAnnotation;
        import com.shk.constant.MessageConstant;
        import com.shk.entity.Result;
        import com.shk.service.MemberService;
        import com.shk.service.OrderService;
        import com.shk.service.ReportService;
        import com.shk.service.UserService;
        import com.shk.utils.DateUtils;
        import org.springframework.web.bind.annotation.RequestBody;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.RestController;

        import java.text.DateFormat;
        import java.text.SimpleDateFormat;
        import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;
    @Reference
    private ReportService reportService;
    @Reference
    private UserService userService;

    /*前端需要数据类型*/
  /*  months: ['2020-08', '2020-08', '2020-08', '2020-08', '2020-08', '2020-08', '2020-08'  日期]
    memberCountt: [820, 932, 901, 934, 1290, 1330, 1320],  人数*/
  /*折线图*/
    @RequestMapping("/getMemberReport")
    public Result linkChart(){
       /*获取时间一年之内的12个月*/
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        List list = new ArrayList<>();
        for(int i=0;i<12;i++){
            /*每次遍历往前推一个月*/
            calendar.add(Calendar.MONTH,1);
            list.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
            }
     Map map = new HashMap();
        map.put("months",list);

        List byOneYear=null;
        try {
            byOneYear = memberService.findByOneYear(list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
        map.put("memberCount",byOneYear);
        return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,map);
    }


    /*饼状图*/
    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        List<Map> nameAndCount = reportService.findNameAndCount();
        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,nameAndCount);
    }

    /*运营数据统计*/

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map data = reportService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
    /*public static void main(String[] args) throws Exception {
        Calendar calendar = Calendar.getInstance();
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));

        calendar.add(Calendar.MONTH,-12);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        System.out.println("1111" +
                "1111");
      List list = new ArrayList<>();
        for(int i=0;i<12;i++){
            calendar.add(Calendar.MONTH,1);

            list.add(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        }
        System.out.println(list);

    }*/
/*
    public static void main(String[] args) {
        String date = "2020-08-31";

    }*/



    //获取指定时间内的 会员信息
    @RequestMapping("/getMemberReportByDate")
    public Result getMemberReportByDate( String start , String end){
        /*System.out.println(start); 2020-1
        System.out.println(end);   2020-12*/
        try {
            Map<String, Integer> memberCountByMonth = reportService.getMemberReportByDate(start,end);
            Set<String> month = memberCountByMonth.keySet();
            ArrayList<String> listMonth = new ArrayList<>();   //   装月份

            //将字符串的日期转换成date
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date dateStart = sdf.parse(start);
            Date dateEnd = sdf.parse(end);
            //将date类型转换文日历类进行操作，获取当前时间
            Calendar bef = Calendar.getInstance();
            Calendar aft = Calendar.getInstance();
            //日历类可以指定时间，指定为用户输入的时间
            bef.setTime(dateStart);
            aft.setTime(dateEnd);
            //计算用户输入时间，月份的差值
            int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
            int Camonth = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
            int total = result+Camonth+1;

            Integer[] number = new Integer[total];    //  每月的人数，如果不够12月，应该也没事

            for (String s : month) {
                listMonth.add(s);
            }
            //将月份集合排序
            Collections.sort(listMonth, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    // 升序
                    return o1.compareTo(o2);
                }
            });

            for (int i = 0; i < listMonth.size(); i++) {
                number[i] = memberCountByMonth.get(listMonth.get(i));
            }
            Map<String, Object> map = new HashMap<>();
            map.put("months", listMonth);
            map.put("memberCount", number);

            // 现在map集合里面包含了 12 月 的数据。问题是前端要的是集合类型的数据
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    /**
     * 饼图
     * 会员性别
     * @return
     */
    @RequestMapping("/getSetmealReportSex")
    @Log4jAnnotation
    public Result getSetmealReportSex() {
        ArrayList<String> sexNames = new ArrayList<>();
        List<Map<String,Object>> sexCounts = memberService.findBySex();
        for (Map<String, Object> map : sexCounts) {
            String name = (String) map.get("name");
            sexNames.add(name);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("sexNames",sexNames);
        hashMap.put("sexCounts",sexCounts);

        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS, hashMap);
    }


    /**
     * 会员年龄
     */
    @RequestMapping("/getSetmealReportAge")
    @Log4jAnnotation
    public Result getSetmealReportAge() {
        ArrayList<String> ageNames = new ArrayList<>();
        List<Map<String,Object>> ageCounts = userService.findByAge();
        for (Map<String, Object> ageCount : ageCounts) {
            String name = (String) ageCount.get("name");
            ageNames.add(name);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("ageNames",ageNames);
        hashMap.put("ageCounts",ageCounts);

        return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,hashMap);
    }
}
